import {Router} from 'express';
import UserService from '../service/user';

const userRouter = Router();

userRouter.get('/', async (req, res) => {
    const users = await new UserService().findAll();
    res.json(users);
});

userRouter.get('/:id', async (req, res) => {
    const user = await new UserService().findOne(parseInt(req.params.id));
    res.json(user);
});

userRouter.post('/', async (req, res) => {
    console.log(req.body)
    const user = await new UserService().create(req.body);
    res.json(user);
});

userRouter.put('/:id', async (req, res) => {
    const user = await new UserService().update(parseInt(req.params.id), req.body);
    res.json(user);
});

userRouter.delete('/:id', async (req, res) => {
    await new UserService().delete(parseInt(req.params.id));
    res.json({message: 'Deleted successfully'});
});

export default userRouter;
